package com.foreknow.elm.controller;

import com.foreknow.elm.po.Orders;
import com.foreknow.elm.service.OrdersService;
import com.foreknow.elm.service.impl.OrdersServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class OrdersController {
    private int a;

    /**
     * 1.根据商家id用户id地址id商品总额 向oders表添加一条数据
     * 1-1根据商家id用户id地址id商品总额 返回编号
     * 2.根据用户id商家id在购物车cart表拿出foodId和quantity添加到订单明细表中orderdetails---返回改变行数
     * 3.根据用户id商家id删除购物车表的数据---返回改变行数
     */
    public Object createOrders(HttpServletRequest request) {
        // 获取请求参数 OrdersController/createOrders?userId=12345671111&businessId=10001&daId=1&orderTotal=47.85
        String userId = request.getParameter("userId");
        Integer businessId = Integer.parseInt(request.getParameter("businessId"));
        Integer daId = Integer.parseInt(request.getParameter("daId"));
        Double orderTotal = Double.parseDouble(request.getParameter("orderTotal"));
        OrdersService ordersService = new OrdersServiceImpl();
        // 调用service方法
        a = ordersService.createOrders(userId, businessId, daId, orderTotal);
        return a;
    }

    /**
     * 根据订单编号查询订单信息，包括所属商家信息，和此订单的所有订单明细信息
     */
    public Object getOrdersById(HttpServletRequest request) {
        // 获取请求参数 OrdersController/getOrdersById?orderId=2
        Integer orderId = Integer.parseInt(request.getParameter("orderId"));
        OrdersService service = new OrdersServiceImpl();
        // 调用service方法
        Orders order = service.getOrdersById(orderId);
        return order;
    }

    /**
     * 根据用户编号查询此用户的所有订单信息
     */
    public Object listOrdersByUserId(HttpServletRequest request) {
        // 获取请求参数 OrdersController/listOrdersByUserId?userId=12345672222
        String userId = request.getParameter("userId");
        OrdersService service = new OrdersServiceImpl();
        // 调用service方法
        List<Orders> list = service.listOrdersByUserId(userId);
        return list;
    }
}
